{template 'common/header'}
<title>我的购物车</title>
<style type="text/css">
	.cart_top {height:44px; width:100%;  background:#f8f8f8;  border-bottom:1px solid #e3e3e3;}
	.cart_top .title {height:44px; width:auto;margin-left:10px; float:left; font-size:16px; line-height:44px; color:#666;}
	.cart_count {height:40px; padding:0 3px; text-align:right; color:#bbb; font-size:14px; line-height:40px;}
	.cart_count span {color:#666;}
	.card_no {height:100px;  margin:50px 0px 60px; color:#ccc; font-size:12px; text-align:center;}
	.card_no_menu {height:40px; width:100%;}
	.card_no_nav {height:38px; width:43%; background:#eee; margin:0px 3%; float:left; border:1px solid #d4d4d4; border-radius:5px; text-align:center; line-height:38px; color:#666;}
	.cart_good .num {height:28px; width:80px; border:1px solid #e2e2e2; margin-top:5px;}
	.cart_good .num .leftnav {height:28px; width:25px; float:left; border-right:1px solid #e2e2e2; background:#eee; color:#6b6b6b; text-align:center; line-height:24px; font-size:20px; font-weight:bold;}
	.cart_good .num .shownum {height:28px; width:26px; float:left; border:0px; margin:0px; padding:0px; text-align:center;}
	.cart_good .num .rightnav {height:28px; width:25px; float:right; border-left:1px solid #e2e2e2; background:#eee; color:#6b6b6b; text-align:center; line-height:24px; font-size:20px; font-weight:bold;}
</style>
<div id='options'></div>
<div id='container'></div>
<script id='option_info' type='text/html'>
	<div class="good_choose_layer"></div>
	<div class="good_choose">
		<div class="info">
			<div class="left">
				<img id="chooser_img" src="<%goods.thumb%>"/>
			</div>
			<div class="right">
				<div class="price">￥<span id='option_price'><%goods.marketprice%></span></div>
				<div class="stock">库存:<span id='option_stock'><%goods.total%></span>件</span> </div>
				<div class="option">请选择规格</div>
			</div>
			<div class="close" onClick="choose2(true)"><i class="fa fa-remove-o"></i></div>
		</div>
		<div class="other">
			<input type='hidden' id='optionid' value='' />
			<%each specs as spec%>
			<input type='hidden' name="optionid[]" class='optionid optionid_<%spec.id%>' value="" title="<%spec.title%>">
			<div class="spec"><%spec.title%></div>
			<div class="spec_items options_<%spec.id%>"  title="<%spec.title%>">
				<%each spec.items as o%>
				<div class="option option_<%spec.id%>" specid='<%spec.id%>' oid="<%o.id%>" sel='false' title='<%o.title%>' thumb='<%o.thumb%>'><%o.title%></div>
				<%/each%>
			</div>
			<%/each%>
		</div>
		<div class="close" onClick="closechoose()"><i class="fa fa-times-circle-o"></i></div>
		<div class="sub <%if specs.length>0%>disabled<%/if%>" onClick="choose2()">确认</div>
	</div>
</script>
<script id='cart_empty' type='text/html'>
	<div class="cart_top">
		<div class="title" onclick='history.back()'><i class='fa fa-chevron-left'></i> 购物车(0)</div>
	</div>
	<div class="card_no"><i class="fa fa-shopping-cart" style="font-size:100px;"></i><br><span style="line-height:18px; font-size:16px;">购物车快饿瘪了</span><br>主人快去给我找点东西吧</div>
	<div class="card_no_menu">
		<div class="card_no_nav" onclick="location.href='{php echo $this->createMobileUrl('member')}'">个人中心</div>
		<div class="card_no_nav"  onclick="location.href='{php echo $this->footer['first']['url']}'">去逛逛</div>
	</div>
</script>
<script id='cart_list' type='text/html'>
	<div class="content-bg-w7">
		<header id="header" class="mui-bar mui-bar-nav">
			<button class="mui-action-back mui-btn mui-btn-blue mui-btn-link mui-btn-nav mui-pull-left header-back-icon">
				<span class="mui-icon mui-icon-left-nav"></span>
				购物车
			</button>
		</header>
		<div style="height:45px;"></div>
		<div class="list-order">
			<ul class="list-group-order">
				<%each list as value%>
				<li class="list-item-order cart_good" data-cartid="<%value.id%>"
					data-marketprice="<%value.marketprice%>"
					data-goodsid="<%value.goodsid%>"
					sel='0'
					data-maxbuy='<%value.maxbuy%>'
					data-stock='<%value.stock%>'>
					<div class="top-tittle-order">
						<span class="mui-icon mui-icon-checkmarkempty unsel-btn" onclick="sel_action(this)"></span>
						<button class="delete-order">
							<span class="mui-icon mui-icon-trash"></span>
						</button>
					</div>
					<div class="mid-content-order">
						<div class="pic-goods-order">
							<img src="<%value.thumb%>" onclick="location.href='{php echo $this->createMobileUrl('shop/detail')}&id=<%value.goodsid%>'" alt="" />
						</div>
						<div class="info-goods-order">
							<div class="goods-name-order" onclick="location.href='{php echo $this->createMobileUrl('shop/detail')}&id=<%value.goodsid%>'"><%value.title%></div>
							<%if value.optionid!='0'%>
							<div class="goods-intro-order optionsel"><%if optiontitle!=''%><%value.optiontitle%><%else%>未选规格<%/if%></div>
							<%/if%>
						</div>
						<div class="info-goods-num-order">
							<div class="goods-money-order">￥<%value.productprice%></div>
							<div class="goods-num-order num"><div class="leftnav">-</div><input type="text" class="shownum" value="<%value.total%>" /><div class="rightnav">+</div>
						</div>
					</div>
					<div class="bottom-goods-order">
						<div class="goods-total-num">共<%value.total%>件商品</div>
						<div class="goods-total-price">实付：<span class="realprice">￥<%value.totalmarketprice%></span></div>
					</div>
				</li>
				<%/each%>
			</ul>
		</div>
		<div class="cart_count">不含运费，共 <span class='total'><%total%></span> 件商品 合计: ￥<span class='totalprice'><%totalprice%></span></div>

		<div class="bottom-fixed-nav-shopcart">
			<div class="nav-flex-box">
				<div class="mui-icon mui-icon-checkmarkempty unsel-btn" onclick="select_all(this)"></div>
				<span>全选</span>
			</div>
			<div class="nav-flex-box">
				<button class="btn-buynow paysub">立即购买</button>
			</div>
		</div>
	</div>
</script>
<script language="javascript">
	function sel_action(e) {
		$(e).toggleClass('selected-btn')
		var this_par = $(e).parents('.list-item-order');
		var this_par_sibling = this_par.siblings();
		var newArr = [];
		for(var i = 0; i < this_par_sibling.length; i++) {
			newArr.push(this_par_sibling.eq(i).find('.unsel-btn').hasClass('selected-btn'))
			setSelect(this_par,this_par.attr('sel'));
		}
		if(this_par.find('.unsel-btn').hasClass('selected-btn') && newArr.indexOf(false)==-1) {
			console.log('全选')
			$('.nav-flex-box').find('.unsel-btn').addClass('selected-btn')
		} else {
			$('.nav-flex-box').find('.unsel-btn').removeClass('selected-btn')
		}
	}

	function select_all(e) {
		$(e).toggleClass('selected-btn')
		if($(e).hasClass('selected-btn')) {
			$('.list-item-order').each(function() {
				var th = $(this);
				$(this).find('.unsel-btn').addClass('selected-btn')
				setSelect(th,th.attr('sel'));
			})
		} else {
			$('.list-item-order').each(function() {
				$(this).find('.unsel-btn').removeClass('selected-btn')
			})
		}

	}

	function choose(){
		$('.good_choose_layer').fadeIn(200);
		$('.good_choose').fadeIn(200);
		$('.good_choose_layer').click(function(){
			closechoose();
		})
	}
	function closechoose(){
		$('.good_choose_layer').fadeOut(100);
		$('.good_choose').fadeOut(100);
	}
	function choose2(direct){


	}
	function setSelect(obj, sel){
		sel =sel==1?0:1;
		obj.attr('sel',sel);
	}
	function calcprice(){
		var total = 0;
		var totalprice = 0;

		$(".cart_good").each(function(){
			var $this = $(this);


			var num = $this.find('.shownum').val();

			if(isNaN(num)){
				num = 1;
			}
			$this.find('.shownum').val(num);
			total+=parseInt(num);
			totalprice+= parseFloat( $this.find('.shownum').val() ) * parseFloat($this.data('marketprice'));
			$this.find('.realprice').html(parseFloat( $this.find('.shownum').val() ) * parseFloat($this.data('marketprice')));

		});


		$('.total').html(total);
		$('.totalprice').html(totalprice.toFixed(2));

		if(total<=0){
			$(".paysub").addClass('disabled');
			$("#tofavorite").addClass('disabled');
			$("#removecart").addClass('disabled');
		}
		else{
			$(".paysub").removeClass('disabled');
			$("#tofavorite").removeClass('disabled');
			$("#removecart").removeClass('disabled');
		}
		return total;
	}

	function option_sel(hasoption){
		var ret= {
			no: "",
			all: []
		};
		if(!hasoption){
			return ret;
		}
		$(".optionid").each(function(){
			ret.all.push($(this).val());
			if($(this).val()==''){
				ret.no = $(this).attr("title");
				return false;
			}
		})
		return ret;
	}

	require(['tpl', 'core'], function(tpl, core) {

		core.json('shop/cart',{},function(json){
			if(json.result.total<=0){
				$('#container').html(  tpl('cart_empty') );
				return;
			}
			$('#container').html(  tpl('cart_list',json.result) );

			$(".cart_good .names,.cart_good .img").click(function(){
				var goodsid = $(this).closest('.cart_good').data('goodsid');
				location.href=core.getUrl('shop/detail',{id:goodsid});
			});

			$('.shownum').blur(function(){

				var maxbuy = parseInt( $(this).closest('.cart_good').data('maxbuy'));
				var stock = parseInt( $(this).closest('.cart_good').data('stock'));

				var input =$(this);
				if(!input.isInt()){
					input.val('1');
					return;
				}
				if(parseInt(input.val())<0){
					input.val('1');
					return;
				}
				var num = parseInt( input.val() );


				if(num>maxbuy && maxbuy>0){
					num=maxbuy;
					mui.toast("您最多购买 " + maxbuy + "件");
				}
				else if(stock!='-1' && stock!='' && num>stock){
					num=stock;
					mui.toast("您最多购买 " + stock + "件");
				}
				input.val(num);

				core.json('shop/cart',{'op':'updatenum',id:$(this).closest('.cart_good').data('cartid'),goodsid:$(this).closest('.cart_good').data('goodsid'), total:num},null,false,true);
				calcprice();


			})

			$('.leftnav').click(function(){

				var input =$(this).next();
				if(!input.isInt()){
					input.val('1');
				}
				var num = parseFloat( input.val() );
				num--;
				if(num<=0){
					num=1;
				}
				input.val(num);

				core.json('shop/cart',{'op':'updatenum',id:$(this).closest('.cart_good').data('cartid'),goodsid:$(this).closest('.cart_good').data('goodsid'), total:num},null,false,true);
				calcprice();
			})

			$('.rightnav').click(function(){
				var maxbuy = parseInt( $(this).closest('.cart_good').data('maxbuy'));
				var stock = parseInt( $(this).closest('.cart_good').data('stock'));

				var input =$(this).prev();
				if(!input.isInt()){
					input.val('1');
				}
				var num = parseInt( input.val() );
				num++;

				if(num>maxbuy && maxbuy>0){
					num=maxbuy;
					mui.toast("您最多购买 " + maxbuy + "件");
				}
				else if(stock!='-1' && stock!='' && num>stock){
					num=stock;
					mui.toast("您最多购买 " + stock + "件");
				}
				input.val(num);

				core.json('shop/cart',{'op':'updatenum',id:$(this).closest('.cart_good').data('cartid'),goodsid:$(this).closest('.cart_good').data('goodsid'), total:num},null,false,true);
				calcprice();
			});

			$('.optionsel').click(function(){
				var id = $(this).closest('.cart_good').data('cartid');
				var goodsid = $(this).closest('.cart_good').data('goodsid');
				core.json('shop/cart',{'op':'selectoption',id:id,goodsid:goodsid},function(json){

					$('#options').html(  tpl('option_info',json.result) );
					$('.good_choose').data('cartid', id);
					$('.good_choose').data('goodsid',goodsid);

					choose();
					var options = json.result.options;

					if(options.length>0) {

						var cartspecs = json.result.cartspecs;
						var cartoption = json.result.cartoption;
						$("#optionid").val(json.result.cartoption.id);
						$(".spec_items").each(function(i){
							$(this).find('.option[oid=' +cartspecs[i] + ']' ).addClass('on');
						});
						var stock =0;
						var marketprice = 0;
						var productprice = 0;

						var len = options.length;
						for(var i=0;i<len;i++) {
							var o = options[i];

							if( o.specs==cartoption.specs){
								optionid = o.id;
								stock = o.stock;
								marketprice = o.marketprice;
								productprice = o.productprice;
								break;
							}
						}
						if(stock==0){
							$('.sub').addClass('disabled').html('库存不足,无法购买');
						}
						else{
							$('.sub').removeClass('disabled').html('确认');
						}



						$("#option_price").html(marketprice);
						$("#option_stock").html(stock);


						$(".spec_items .option").click(function() {
							var specid = $(this).attr("specid");
							var oid = $(this).attr("oid");
							$(".optionid_"+specid).val(oid);
							$(".options_" + specid + "  .option").removeClass("on").attr("sel", "false");
							$(this).addClass("on").attr("sel", "true");
							var titles='已选: ';
							$('.spec_items').each(function(){
								if($(this).find('.on').length>0){
									titles+= $(this).find('.on').attr('title')+";";
								}
							});

							$('.good_choose .info .right .option').html(titles);
							var thumb = $(this).attr('thumb');
							if(thumb!=''){
								$("#chooser_img").attr('src',thumb);
							}
							else{

								$("#chooser_img").attr('src',json.result.goods.thumb);
							}
							var optionid = "";
							var stock =0;
							var marketprice = 0;
							var productprice = 0;
							var ret = option_sel(json.result.options.length>0);

							if(ret.no==''){
								var len = options.length;
								for(var i=0;i<len;i++) {
									var o = options[i];
									var ids = ret.all.join("_");
									if( o.specs==ids){
										optionid = o.id;
										stock = o.stock;
										marketprice = o.marketprice;
										productprice = o.productprice;
										break;
									}

								}
								$("#optionid").val(optionid);

								if(stock!="-1"){
									$("#stockcontainer").html("库存:<span id='stock'>" + stock + "</span>");
								}
								else{
									$("#stockcontainer").html("<span id='stock'></span>");
								}
								if(ret.no==''){
									if(stock==0){
										$('.sub').addClass('disabled').html('库存不足,无法购买');
									}
									else{
										$('.sub').removeClass('disabled').html('确认');
									}
								}


								$("#option_price").html(marketprice);
								$("#option_stock").html(stock);

							}
						});
					}

					$('.sub').click(function(){

						if($("#optionid").val()==''){
							return;
						}
						var id = $(this).closest('.good_choose').data('cartid');
						var goodsid = $(this).closest('.good_choose').data('goodsid');
						var optionid = $('#optionid').val();


						core.json('shop/cart',{'op':'setoption',id:id,goodsid:goodsid,optionid:optionid},function(json){
							if(json.status==1){
								closechoose();

								$('.cart_good[data-cartid=' + id + ']').find('.cinner').html(json.result.optiontitle);
							}
							else{
								mui.toast('设置失败');
							}

						},true,true);

					});


				},true);

			});

			$('.delete-order').click(function(){
				var th = $(this).parent().parent();
				var ids = [];

				ids.push(th.data('cartid'));
				console.log(th);
				console.log(ids);
				var btnArray = ['是', '否'];
				mui.confirm('确认删除该订单？', '温馨提示', btnArray, function(e) {
					if(e.index == 0) {
						$('.cart_good').attr('del',0);
						core.json('shop/cart',{'op':'remove',ids:ids},function(json){
							if(json.status==1)  {
								for(var i in ids){
									$('.cart_good[data-cartid=' + ids[i]+ ']').attr('del',1).fadeOut(500,function(){
										$('.cart_good[data-cartid=' + ids[i]+ ']').remove();
									})
								}
								if($('.cart_good[del=0]').length<=0){
									$('#container').html(  tpl('cart_empty') );
								}else{
									calcprice();
								}
							}else{
								mui.toast('删除失败');
							}
						},true,true);
					}
				});
			});

			$('.paysub').click(function(){

				var total =  calcprice();
				if(total<=0){
					return;
				}
				var ids = [];
				$('.cart_good[sel=1]').each(function(){
					ids.push($(this).data('cartid'));
				})

				location.href = core.getUrl('order/confirm',{cartids:ids.join(',')});
			})



		});
	});
</script>
{template 'common/footer'}